package com.lz.fruitday.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.lidroid.xutils.exception.DbException;
import com.lz.fruitday.BaseApp;
import com.lz.fruitday.R;
import com.lz.fruitday.bean.Good;
import com.lz.fruitday.bean.QiangxianProduct;
import com.lz.fruitday.utils.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by liuZhi
 * 2015/10/20
 */
public class QXAdapter extends AppBaseAdapter<QiangxianProduct>{
    DisplayImageOptions options=null;
    public QXAdapter(List<QiangxianProduct> list, Context context) {
        super(list, context);
        options= ImageLoaderUtil.getDefaultOptions();
    }

    @Override
    public ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        final QiangxianProduct product=list.get(position);
        ViewHolder vh=ViewHolder.get(context,convertView,parent, R.layout.item_qx_listview_layout);
        TextView tvTime= (TextView) vh.findViewById(R.id.tv_distance_time);
        ImageView ivPhoto= (ImageView) vh.findViewById(R.id.iv_thum_photo);
        TextView tvName= (TextView) vh.findViewById(R.id.tv_product_name);
        TextView tvVolume= (TextView) vh.findViewById(R.id.tv_volume);
        TextView tvPrice= (TextView) vh.findViewById(R.id.tv_price);
        TextView tvImg= (TextView) vh.findViewById(R.id.tv_img);

        tvTime.setText("距结束123:45:678");
        ImageLoader.getInstance().displayImage(product.getThum_photo(), ivPhoto, options);
        tvName.setText(product.getProduct_name());
        tvVolume.setText(product.getVolume());
        tvPrice.setText(product.getPrice()+"元");
        tvImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Good good=new Good();
                good.setName(product.getProduct_name());
                good.setPrice(product.getPrice());
                good.setUrl(product.getThum_photo());
                good.setNum(1);
                try {
                    BaseApp.getInstance().getDbUtils().save(good);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                Toast.makeText(context,"保存到数据库",Toast.LENGTH_SHORT).show();
            }
        });

        return vh;
    }
}
